package com.bigfish.gbs.controller;

import com.bigfish.gbs.dto.OrderItemVO;
import com.bigfish.gbs.dto.Result;
import com.bigfish.gbs.entity.OrderInfo;
import com.bigfish.gbs.entity.OrderItem;
import com.bigfish.gbs.enums.ResultEnum;
import com.bigfish.gbs.service.OrderService;
import com.bigfish.gbs.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Joycessh
 * @Description
 * @Date 12/24/18 11:23 AM
 */
@Api(value = "订单模块", description = "与订单相关的")
@RestController
@RequestMapping("/order")
public class OrderController extends BasicAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsCtroller.class);

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "创建订单", httpMethod = "POST")
    @PostMapping("")
    public Result createOrder(@RequestBody List<OrderItem> list) {

        Long orderId = orderService.createOrder(list);
        if (orderId != null) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("orderId", orderId);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }

    @ApiOperation(value = "获取订单信息", httpMethod = "GET")
    @GetMapping("")
    public Result getOrderList(@RequestParam String userId) {
        List<OrderItemVO> list = orderService.getOrderList(userId);
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("order", list);
    }

    @ApiOperation(value = "获取订单信息", httpMethod = "GET")
    @GetMapping("/list")
    public Result getOrder(@RequestParam Integer currentPage,
                           @RequestParam Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<OrderInfo> list = orderService.getOrders();
        PageInfo pageInfo =new PageInfo(list);
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("orders", pageInfo);
    }
}
